package org.jwebuilder.parsers;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Inject;
import com.google.inject.name.Named;

/**
 * 模板解析器
 * @author Xu Jin
 */
public abstract class TemplateParser
{
	protected ServletContext application;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected boolean enableTemplateCache;
	protected String pageId;

	@Inject
	public void setApplication(ServletContext application)
	{
		this.application = application;
	}
	@Inject
	public void setRequest(HttpServletRequest request)
	{
		this.request = request;
	}
	@Inject
	public void setResponse(HttpServletResponse response)
	{
		this.response = response;
	}
	@Inject
	public void setEnableTemplateCache(@Named("EnableTemplateCache") boolean enableTemplateCache)
	{
		this.enableTemplateCache = enableTemplateCache;
	}

	public String getPageId()
	{
		return pageId;
	}
	public void setPageId(String pageId)
	{
		this.pageId = pageId;
	}

	/**
	 * 解析模板
	 * @param template 模板字符串
	 * @param contextObj 上下文对象
	 * @param vars 外部变量
	 * @param templateEncoding 模板文件编码
	 * @return
	 */
	public abstract String parse(String template, Object contextObj, Map<String, Object> vars, String templateEncoding);

	/**
	 * 解析模板
	 * @param templateFile 模板文件
	 * @param contextObj 上下文对象
	 * @param vars 外部变量
	 * @param templateEncoding 模板文件编码
	 * @return
	 * @throws IOException
	 */
	public abstract String parse(File templateFile, Object contextObj, Map<String, Object> vars, String templateEncoding) throws IOException;
}